package org.wanda.pv.controller.basic;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.framework.page.Pagination;
import org.wanda.pv.model.basic.Specialty;
import org.wanda.pv.service.basic.SpecialtyService;

import java.util.List;

/**
 * @author LiuXuyang
 * Created by lxy on 2018/5/10.
 */
@RestController
@RequestMapping("/basic/scate")
public class SpecialtyController {
    private final static Logger log = LoggerFactory.getLogger(SpecialtyController.class);

    @Autowired
    Dao dao;

    @Autowired
    private SpecialtyService specialtyService;

    /**
     * @api {post} /basic/scate/add 添加专业
     * @apiParam {String} name 专业名称
     * @apiGroup 专业分类
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(Specialty specialty){
        try{
            Result result=specialtyService.add(specialty);
            return result;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("fali","服务器内部错误！");
        }
    }
    /**
     * @api {post} /basic/scate/del 删除专业
     * @apiParam {String} id 专业ID
     * @apiGroup 专业分类
     */
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public Object del(@RequestParam("id")Long id){
        try{

            Result result=specialtyService.del(id);
            return result;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("fali","服务器内部错误！");
        }
    }


    /**
     * @api {post} /basic/scate/update 修改专业
     * @apiParam {String} id 专业ID
     * @apiParam {String} name 专业名称
     * @apiGroup 专业分类
     */

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(Specialty specialty){
        try{
            Result result=specialtyService.updateSpec(specialty);
            return result;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("fali","服务器内部错误！");
        }
    }


    /**
     * @api {post} /basic/scate/list 分页查询所有数据
     * @apiParam {String} pageSize 当前页码(默认值为1)
     * @apiParam {String} pageNum 单页显示条数(默认值为10)
     * @apiParam {String} name 专业名称(默认值为空)
     * @apiGroup 专业分类
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "name", defaultValue = "") String name) {
        try {
            Cnd cnd = Cnd.where("name", "like", "%" + name + "%");
            Pagination listPage = specialtyService.listPage(pageNum, pageSize, cnd
                    .and("delFlag", "=", "0"));
            List<Specialty> list = (List<Specialty>) listPage.getList();

            return Result.success("success", listPage);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("fail", "服务器内部错误!");
        }
    }


    /**
     * @api {post} /basic/scate/getAll 获取所有专业
     * @apiParam {String} name 专业名称(默认值为空)
     * @apiGroup 专业分类
     */
    @RequestMapping(value = "/getAll",method = RequestMethod.POST)
    public Object getAll( @RequestParam(value = "name",defaultValue = "")String name){
        try{
            List<Specialty> listPage = specialtyService.query(Cnd.where("name", "like", "%" + name + "%")
                    .and("delFlag","=","0"));
            return Result.success("success",listPage);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return Result.error("fail","服务器内部错误!");
        }

    }


}
